package com.bruceliu.controller;

import com.bruceliu.bean.*;
import com.bruceliu.service.CartService;
import com.bruceliu.service.FoodTypeService;
import com.bruceliu.service.ItemService;
import com.bruceliu.service.OrderService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @BelongsProject: RestaurantSystem
 * @BelongsPackage: com.bruceliu.controller
 * @Author: bruceliu
 * @QQ:1241488705
 * @CreateTime: 2020-03-01 23:53
 * @Description: TODO
 */
@Controller
@Scope("prototype")
public class OrderController {

    @Resource
    OrderService orderService;

    @Resource
    CartService cartService;

    @Resource
    ItemService itemService;

    @Resource
    FoodTypeService foodTypeService;

    /**
     * 用户下单
     */
    @RequestMapping("/genernateOrder")
    public void genernateOrder(HttpSession session, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        Desk desk = (Desk) session.getAttribute("desk");
        int size = cartService.getCarts(desk.getId()).size();
        int desk_id = desk.getId();
        int count = orderService.addOrder(desk_id);
        if (count == size + 1) {
            response.getWriter().write("<script>alert('下单成功！');location.href='/findOrders';</script>");
        } else {
            response.getWriter().write("<script>alert('下单失败！');location.href='/my_carts';</script>");
        }
    }

    /**
     * 订单展示页面
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/findOrders")
    public String findOrders(HttpSession session, Model model){
        Desk desk = (Desk) session.getAttribute("desk");
        int desk_id = desk.getId();

        Order order = orderService.getByDeskId(desk_id);
        List<Item> items = itemService.findItems(order.getOid());

        //所有的菜系信息
        List<FoodType> foodTypes = foodTypeService.getAllFoodTypes();
        model.addAttribute("foodTypes",foodTypes);

        model.addAttribute("order",order);
        model.addAttribute("items",items);
        return "clientOrderList";
    }

    /**
     * 后台查询订单数据
     * @return
     */
    @RequestMapping("/getOrders")
    public String getOrders(Model model){
        List<Order> orders = orderService.getOrders();
        model.addAttribute("orders",orders);
        return "orderList";
    }

    /**
     * 获取订单详情
     * @return
     */
    @RequestMapping("/getItems/{oid}")
    public String getItems(@PathVariable("oid") int oid,Model model){
        List<Item> items = itemService.findItems(oid);
        model.addAttribute("items",items);
        return "orderDetail";
    }

}
